//
//  EditBabyActivityView.m
//  BabyHelper
//
//  Created by  thomas on 11-2-11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EditBabyActivityView.h"


@implementation EditBabyActivityView
@synthesize unitLabel,amountLabel,amountTextField,commentsLabel,commentsTextField,datePicker,categoryControl;
@synthesize currentBabyActivityEntry;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	self.title = NSLocalizedString(@"Edit Records",@"");

	//set default value:
	[self.categoryControl setTitle:NSLocalizedString(@"Mum",@"")     forSegmentAtIndex:0];
	[self.categoryControl setTitle:NSLocalizedString(@"Milk",@"")    forSegmentAtIndex:1];
	[self.categoryControl setTitle:NSLocalizedString(@"Water",@"") forSegmentAtIndex:2];
	[self.categoryControl setTitle:NSLocalizedString(@"Pee",@"")         forSegmentAtIndex:3];
	[self.categoryControl setTitle:NSLocalizedString(@"Shit",@"")        forSegmentAtIndex:4];		

	self.commentsLabel.text            = NSLocalizedString(@"Notes",@"");
	self.commentsTextField.placeholder = NSLocalizedString(@"Please input notes...",@"");
	self.amountLabel.text			   = NSLocalizedString(@"Duration",@"");
	
    [super viewDidLoad];
	
	UIBarButtonItem *saveItem = [[UIBarButtonItem alloc]
								 initWithTitle:NSLocalizedString(@"Done",@"")
								 style:UIBarButtonItemStyleBordered 
								 target:self
								 action:@selector(toggleSave)		
								 ];
	self.navigationItem.rightBarButtonItem = saveItem;
	[saveItem release];
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
	if(self.currentBabyActivityEntry != nil )
	{
		self.categoryControl.selectedSegmentIndex = self.currentBabyActivityEntry.category;
		[self.categoryControl setEnabled:YES forSegmentAtIndex:self.currentBabyActivityEntry.category];
		self.amountTextField.text   = [NSString stringWithFormat:@"%d",self.currentBabyActivityEntry.amount];
		self.commentsTextField.text = self.currentBabyActivityEntry.comments;
		[self.datePicker setDate:self.currentBabyActivityEntry.timestamp];
	} 
	else { 
		[self.datePicker setDate:[NSDate date] animated:YES];
	}
}

-(void)viewWillAppear:(BOOL)animated{
	

}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.currentBabyActivityEntry = nil;
	self.amountLabel= nil;
	self.amountTextField= nil;
	self.commentsLabel= nil;
	self.commentsTextField= nil;
	self.datePicker= nil;
	self.categoryControl= nil;
}

-(IBAction)backgroundTap:(id)sender{
	[commentsTextField resignFirstResponder];
	[amountTextField resignFirstResponder];
}

-(IBAction)toggleControls:(id)sender{
	
	if ([sender selectedSegmentIndex] == EAT_MUM_CATEGORY) {
		self.amountLabel.hidden     = false;
		self.unitLabel.hidden       = false;
		self.amountLabel.text		= NSLocalizedString(@"Duration",@"");
		self.unitLabel.text		    = NSLocalizedString(@"min",@"");
		self.amountTextField.text   = @"";
		self.amountTextField.hidden = false;
	}else if ([sender selectedSegmentIndex] == EAT_MILK_CATEGORY) {
		self.amountLabel.hidden     = false;
		self.unitLabel.hidden       = false;
		self.amountLabel.text		= NSLocalizedString(@"Amount",@"");
		self.unitLabel.text		    = NSLocalizedString(@"ml",@"");
		self.amountTextField.text   = @"";
		self.amountTextField.hidden = false;		
	}else if ([sender selectedSegmentIndex] == DRINK_WATER_CATEGORY) {
		self.amountLabel.hidden     = false;
		self.amountLabel.text		= NSLocalizedString(@"Amount",@"");
		self.unitLabel.text		    = NSLocalizedString(@"ml",@"");
		self.amountTextField.text   = @"";
		self.amountTextField.hidden = false;		
	}else if ([sender selectedSegmentIndex] == PEE_CATEGORY) {
		self.amountLabel.hidden     = true;
		self.unitLabel.hidden       = true;
		self.amountLabel.text       = @"";
		self.amountTextField.hidden = true;
		self.amountTextField.text   = @"";
	}else if ([sender selectedSegmentIndex] == SHIT_CATEGORY) {
		self.amountLabel.hidden     = true;
		self.unitLabel.hidden       = true;
		self.amountLabel.text       = @"";
		self.amountTextField.hidden = true;
		self.amountTextField.text   = @"";		
	}else {
		return;
	}
}

-(IBAction)toggleSave{
	if([self DataValidation] == YES)
    {
		[self SaveBabyActivityEntry];
		[self.navigationController popViewControllerAnimated:true];		
	}
}

-(BOOL)DataValidation{
	Dao *dao = [Dao getInstance];
	BabyProfile *babyProfile = [[BabyProfile alloc]init];
	[dao LoadBabyProfile:babyProfile];
	if (babyProfile.Birthday > [[self.datePicker date] timeIntervalSince1970]) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"")
														message:NSLocalizedString(@"Input date should not early than birthday",@"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		[babyProfile release];
		return NO;
	}
	[babyProfile release];
	
	if([self.commentsTextField.text length] > 100)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"")
														message:NSLocalizedString(@"Input text too long",@"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		return NO;
	}

	if([self.amountTextField.text intValue] > 2000)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"")
														message:NSLocalizedString(@"Invalid input value",@"")
													   delegate:nil 
											  cancelButtonTitle:@"OK"
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
		return NO;
	}
	return YES;
}


-(void)SaveBabyActivityEntry{	
	Dao *dao = [Dao getInstance];
	BabyActivityEntry *entry = [[BabyActivityEntry alloc]init];

	entry.category  = [self.categoryControl selectedSegmentIndex];
	entry.timestamp = [self.datePicker date];
	entry.amount    = [self.amountTextField.text intValue];
	entry.comments  =  self.commentsTextField.text;
	
	if(self.currentBabyActivityEntry == nil )
	{
		[dao InsertBabyActivity:entry];
	}else {
		entry.index = self.currentBabyActivityEntry.index;
		[dao UpdateBabyActivityEntry:entry];
	}
	[entry release];	
}

- (void)dealloc {
	self.currentBabyActivityEntry = nil;
	self.amountLabel= nil;
	self.amountTextField= nil;
	self.commentsLabel= nil;
	self.commentsTextField= nil;
	self.datePicker= nil;
	self.categoryControl= nil;
	[super dealloc];
}

@end
